﻿using Authority.DataTransferObject.ActionAuthority;
using Authority.Doamin;
using Authority.Doamin.IRepository;
using Authority.EFRepositoryImpl;
using Authority.IServices;
using Authority.IServices.Model.Action;
using AutoMapper;
using HelperFromTheSkeleton.Convert;
using HelperFromTheSkeleton.Enums;
using HelperFromTheSkeleton.ExtendMethod;
using HelperFromTheSkeleton.Model.PageModel;
using System.Linq.Expressions;

namespace Authority.ServiceImpl
{
    /// <summary>
    /// 功能权限服务
    /// </summary>
    public class ActionAuthorityServiceImpl: IActionAuthorityService
    {
        private readonly IActionAuthorityRepository _actionAuthorityRepository;
        private readonly ISubsystemRepository _subsystemRepository;
        private readonly IRoleAuthorityActionRepository _roleAuthorityRepository;
        private readonly IRoutingCarryActionRepository _routingCarryActionRepository;
        private readonly IUnitWork _unitWork;
        private readonly IMapper _mapper;

        public ActionAuthorityServiceImpl(IUnitWork unitWork, IMapper mapper, IActionAuthorityRepository actionAuthorityRepository, ISubsystemRepository subsystemRepository, IRoleAuthorityActionRepository roleAuthorityRepository, IRoutingCarryActionRepository routingCarryActionRepository)
        {
            _actionAuthorityRepository = actionAuthorityRepository;
            _unitWork = unitWork;
            _mapper = mapper;
            _subsystemRepository = subsystemRepository;
            _roleAuthorityRepository = roleAuthorityRepository;
            _routingCarryActionRepository = routingCarryActionRepository;
        }

        /// <summary>
        /// 新增功能权限
        /// </summary>
        /// <param name="model"></param>
        /// <exception cref="InvalidOperationException"></exception>
        public async Task AddAsync(AddActionModel model)
        {
            if (string.IsNullOrEmpty(model.Code)) throw new InvalidOperationException("功能Code不能为空");
            if (string.IsNullOrWhiteSpace(model.Name)) throw new InvalidOperationException("功能名称不能为空");
            if (model.SubsystemID == Guid.Empty) throw new InvalidOperationException("子系统ID不能为空");
            if(await _subsystemRepository.CountAsync(m=>m.ID == model.SubsystemID)<=0) throw new InvalidOperationException("无效的子系统");
            if (await _actionAuthorityRepository.CountAsync(m => (m.Name == model.Name || m.Code == model.Code) && m.SubsystemID == model.SubsystemID) > 0) throw new InvalidOperationException("子系统内的功能名称、编号不允许重复");
            ActionAuthority dbInfo = _mapper.Map<ActionAuthority>(model);
            _unitWork.Add(dbInfo);
            await _unitWork.CommitAsync();
        }

        /// <summary>
        /// 修改功能权限
        /// </summary>
        /// <param name="model"></param>
        /// <exception cref="InvalidOperationException"></exception>
        public async Task EditAsync(EditActionModel model)
        {
            if (string.IsNullOrEmpty(model.Code)) throw new InvalidOperationException("功能Code不能为空");
            if (string.IsNullOrWhiteSpace(model.Name)) throw new InvalidOperationException("功能名称不能为空");
            if (model.ID == Guid.Empty) throw new InvalidOperationException("ID不能为空");
            ActionAuthority dbInfo = await _actionAuthorityRepository.FirstAsync(m => m.ID == model.ID) ?? throw new InvalidOperationException("找不到此功能权限");
            if (await _actionAuthorityRepository.CountAsync(m => (m.Name == model.Name || m.Code == model.Code) && m.SubsystemID == dbInfo.SubsystemID && m.ID != model.ID) > 0) throw new InvalidOperationException("子系统内的功能名称、编号不允许重复");
            model.CopyProperty(dbInfo);
            _unitWork.Updata(dbInfo);
            await _unitWork.CommitAsync();
        }

        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public async Task DeleteAsync(Guid id)
        {
            var dbInfo = await _actionAuthorityRepository.FirstAsync(m => m.ID == id) ?? throw new InvalidOperationException("找不到此功能权限");
            if (await _routingCarryActionRepository.CountAsync(m => m.ActionAuthorityID == id) > 0) throw new InvalidOperationException("有路由正在关联此功能");
            if (await _roleAuthorityRepository.CountAsync(m => m.ActionAuthorityID == id) > 0) throw new InvalidOperationException("有角色正在关联此功能");
            _unitWork.Delete(dbInfo);
            await _unitWork.CommitAsync();
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public async Task<List<ActionAuthoritytDTO>> GetAsync(List<Guid> id)
        {
            List<ActionAuthority> dbInfo = await _actionAuthorityRepository.FindAsync(m => id.Contains(m.ID),null);
            var result  = _mapper.Map<List<ActionAuthoritytDTO>>(dbInfo);
            return result;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<(List<ActionAuthorityListDTO>, PageResultModel)> QueryAsync(QueryActionModel model)
        {
            Expression<Func<ActionAuthority, bool>> expression = m => m.SubsystemID == model.SubsystemID;
            if (!string.IsNullOrWhiteSpace(model.Name)) expression = expression.And(m => m.Name == model.Name);
            if (!string.IsNullOrWhiteSpace(model.Code)) expression = expression.And(m => m.Code == model.Code);
            (List<ActionAuthority> dbInfos, PageResultModel page) = await _actionAuthorityRepository.PageFindAsync(model, expression, m => m.Name, OrderByEnum.Asc);
            var result = _mapper.Map<List<ActionAuthorityListDTO>>(dbInfos);
            return(result, page);
        }
    }

}